package com.lw.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.staservice.client.UcenterClient;
import com.lw.staservice.entity.StatisticsDaily;
import com.lw.staservice.mapper.StaMapper;
import com.lw.staservice.service.StaService;
import com.lw.staservice.utils.DateUtil;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StaServiceImpl extends ServiceImpl<StaMapper, StatisticsDaily> implements StaService {

    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public void registerCount(String day) {
        //查询数据库是否有当天数据
        // 如果有 先删除 在添加最新数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq( "date_calculated",day );
        baseMapper.delete( wrapper );

        //远程调用得到当天注册人数
        Integer countRegister = ucenterClient.countRegister( day );

        //统计数据添加至数据库
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum( countRegister ); //注册人数
        daily.setDateCalculated( day ); //统计日期


        daily.setVideoViewNum( RandomUtils.nextInt( 100,500 ) );
        daily.setLoginNum( RandomUtils.nextInt( 100,600 ) );
        daily.setCourseNum( RandomUtils.nextInt( 100,700 ) );
        baseMapper.insert( daily );
    }

    @Override
    public Map showData(String type, String begin, String end) {
        if (StringUtils.isEmpty( type )){
            type = "login_num";
        }
        if (StringUtils.isEmpty( begin )){
            begin= DateUtil.formatDate( DateUtil.addDays( new Date(),-7 ) );
        }
        if (StringUtils.isEmpty( end )){
            end=DateUtil.formatDate( new Date() ); //默认查询前一周的注册人数
        }
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between( "date_calculated",begin,end );
        wrapper.orderByAsc( "date_calculated" );
        List<StatisticsDaily> dailyList = baseMapper.selectList( wrapper );
        //封装两部分数据
        List<String> dates = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        for (int i=0;i<dailyList.size();i++){
            StatisticsDaily statisticsDaily = dailyList.get( i );
            dates.add( statisticsDaily.getDateCalculated() );
            switch (type){
                case "login_num":
                    counts.add( statisticsDaily.getLoginNum() );
                    break;
                case "register_num":
                    counts.add( statisticsDaily.getRegisterNum() );
                    break;
                case "video_view_num":
                    counts.add( statisticsDaily.getVideoViewNum() );
                    break;
                case "course_num":
                    counts.add( statisticsDaily.getCourseNum() );
                    break;
            }
        }
        Map map = new HashMap();
        map.put( "dates",dates );
        map.put( "counts",counts );
        return map;
    }
}
